﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
using System.Web.Helpers;
using System.Web;
namespace GPMS.Model.Validator
{
    public class RemoteValidator:DataAnnotationsModelValidator<RemoteCustom>
    {
        private string url;
        private string error;
        private string value;
        public RemoteValidator(ModelMetadata metadata, ControllerContext context, RemoteCustom attr) :
            base(metadata, context, attr)
        {
            error = attr.ErrorMessage;
            value = metadata.Model == null ? String.Empty : metadata.Model.ToString();
            UrlHelper uh = new UrlHelper(context.RequestContext);
            url = uh.Action(attr.Action, attr.Controller);
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rules = new List<ModelClientValidationRule>();
            var rule = new ModelClientValidationRule()
            {
                ErrorMessage = error,
                ValidationType = "remote",
            };
           
            rule.ValidationParameters.Add("url", url);
            rule.ValidationParameters.Add("dataType", "json");
            rule.ValidationParameters.Add("data", new { key=value});
            rule.ValidationParameters.Add("type", "post");
            rules.Add(rule);
            return rules;
        }
    }
}
